package com.shengqi.xinnongren.work.ui.goods.presenter;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.gson.Gson;
import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.Global;
import com.shengqi.xinnongren.work.ui.goods.view.ConfirmOrderView2;
import com.shengqi.xinnongren.work.ui.user.model.PrepareOrderInfoBean;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class ConfirmOrder2Presenter extends BasePresenter<ConfirmOrderView2> {


    /**
     * 获取预订单信息
     */
    //    "{\"comId\":\"1,2\",\"couponId\":\"1\",\"userIntegral\":\"1\"}"
    public void getPrepareOrderInfo(@NonNull String comId, @Nullable String couponId, int useIntegral) {
        Map<String, Object> map = new HashMap<>();
        map.put("comId", comId);
        map.put("couponId", couponId);
        map.put("userIntegral", useIntegral);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        getView().showLoadingView();
        addTask(RetrofitHelper.getInstance().getService().getPrepareOrderInfo(requestBody), s -> {
            getView().hideLoadingView();
            HttpRespond<String> httpRespond = Global.getHttpResult(s);
            if (httpRespond.result == 1) {
                PrepareOrderInfoBean prepareOrderInfoBean = new Gson().fromJson(httpRespond.data, PrepareOrderInfoBean.class);
                getView().onGetPrepareOrderInfo(prepareOrderInfoBean);
            } else {
                getView().onRequestFailed(httpRespond.message);
            }
        });
    }

    /**
     * 提交订单
     */
    //    {\"addressId\":\"1\",\"comId\":\"2\",\"timeId\":\"6\",\"couponId\":\"6\",\"userIntegral\":\"\",\"remark\":\"备注\",}
    public void commitOrder(String addressId, String comId, String timeId, String couponId, int useIntegral, String remark) {
        Map<String, Object> map = new HashMap<>();
        map.put("addressId", addressId);
        map.put("comId", comId);
        map.put("timeId", timeId);
        map.put("couponId", couponId);
        map.put("userIntegral", useIntegral);
        map.put("remark", remark);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        getView().showLoadingView();
        addTask(RetrofitHelper.getInstance().getService().commitOrder(requestBody), s -> {
            getView().hideLoadingView();
            HttpRespond<String> httpRespond = Global.getHttpResult(s);
//            "orderId": 36, "payMoney": 32.6
            if (httpRespond.result == 1) {
                getView().onOrderCommit(new JSONObject(httpRespond.data).optString("orderId"), httpRespond.message);
            } else {
                getView().onRequestFailed(httpRespond.message);
            }
        });
    }
}
